"""Tag schemas for API validation."""

from __future__ import annotations

from datetime import datetime
from pydantic import BaseModel, Field, ConfigDict


class TagBase(BaseModel):
    """Tag基础schema."""

    name: str = Field(..., min_length=1, max_length=50, description="标签名称")
    category: str = Field(default="custom", description="分类：facility/environment/special/custom")
    level: int = Field(default=2, ge=1, le=3, description="优先级（1-3星）")
    keywords: str | None = Field(None, max_length=200, description="关键词（逗号分隔）")
    description: str | None = Field(None, max_length=200, description="标签描述")
    is_active: bool = Field(default=True, description="是否启用")


class TagCreate(TagBase):
    """创建Tag的schema."""

    pass


class TagUpdate(BaseModel):
    """更新Tag的schema（所有字段可选）."""

    name: str | None = Field(None, min_length=1, max_length=50)
    category: str | None = None
    level: int | None = Field(None, ge=1, le=3)
    keywords: str | None = Field(None, max_length=200)
    description: str | None = Field(None, max_length=200)
    is_active: bool | None = None


class TagInDB(TagBase):
    """Tag数据库schema（包含所有字段）."""

    model_config = ConfigDict(from_attributes=True)

    id: int
    usage_count: int
    created_at: datetime
    updated_at: datetime


class TagResponse(TagInDB):
    """Tag响应schema."""

    pass


class TagStatistics(BaseModel):
    """标签统计信息."""

    total: int = Field(..., description="标签总数")
    active: int = Field(..., description="启用的标签数")
    by_category: dict[str, int] = Field(..., description="按分类统计")
    by_level: dict[str, int] = Field(..., description="按优先级统计")
